package com.qk.management.handler;

import com.qk.common.exception.BizException;
import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:gzc
 * @Date: 2025/10/06 16:49
 * @Description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("全局异常处理器拦截到异常", e);
        return Result.error("您的网络有问题,请稍后重试");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        log.error("重复值异常处理器拦截到异常", e);
        String message = e.getMessage();
        String[] split = message.substring(message.lastIndexOf("Duplicate entry")).split(" ");
        return Result.error(split[2]+"已存在");
    }

    @ExceptionHandler(BizException.class)
    public Result handleBizException(BizException e) {
        log.error("自定义异常处理器拦截到异常：", e);
        return Result.error(e.getMessage());
    }

}
